package Monster;

import java.awt.Color;

import MainClass.Game;
import SubClasses.Direction;
import SubClasses.TypeOfVar;

public class Outside extends Monster
{
	public Outside(int xPos,int yPos,Direction x,Direction y,int s, int q)
	{
		Pos_X = xPos;
		Pos_Y= yPos;
				
		DirectionAxisX=x;
		DirectionAxisY=y; 
		Step=s; 
		Quantum=q; 
		type = TypeOfVar.FrameFill;
		ascii = '#';
		Forg = Color.BLACK;	
		
		game = Game.GameBuilder();
	}
	
	public Outside(int s)
	{
		game = Game.GameBuilder();
		
		int pos[] = game.randomLocationOutside();
		Pos_X = pos[0];
		Pos_Y= pos[1];
		
		DirectionAxisX=game.GetUpDounDirection();
		DirectionAxisY=game.GetLeftRightDirection(); 
		
		Step=Quantum=s;
		//sQuantum=++startStep%Step;
		
		ascii = '#';
		Forg = Color.BLACK;		
	}
	
	@Override
	public void UpdateMonstersPosition() 
	{
		int newX[] = new int[8];
		int newY[] = new int[8];
		
		if (Quantum  > 0 ) 
		{
		   (Quantum )--;
		   return;
		}
		Quantum  = Step ;
		
		createAllPossibleOption(newX, newY);
	

		int i;
		for(i=0;i<8;i++)
		{
			if(!game.CheckIndax(newX[i], newY[i])) continue;
			if(game.isFrameNotFillIndex(newX[i], newY[i])) continue;
			if (game.isMonsterIndex(newX[i], newY[i])) continue; 
			if(game.isUserIndex(newX[i], newY[i])) game.strike();
			break;
		}
		if(i==8)
		{
			return;
		}
		//System.out.println("i = " + i );
		 
		this.DirectionAxisX = NewDirectionAxisX(i);
		this.DirectionAxisY = NewDirectionAxisY(i);
		
		game.SetInBoardValues(Pos_X,Pos_Y,null,TypeOfVar.Empty,TypeOfVar.FrameFill);
		Pos_X = newX[i];
		Pos_Y= newY[i];
		//System.out.println(" Conse x = " + Pos_X + " Y = " + Pos_Y);
		game.SetInBoardValues(Pos_X,Pos_Y,this,TypeOfVar.Monster,TypeOfVar.FrameFill);
			
	}


	
	private boolean isLegalPosition(int i,int j)
	{
		if(game.CheckIndax(i, j) && game.isEmptyIndex(i, j) && game.isFrameFillIndex(i, j))
			return true;
		return false;
	}
	
	public void pr(int newX[],int newY[])
	{
		int i;
		for(i=0;i<8;i++)
		{
			System.out.println( newX[i] + "  " + newY[i] );
		}
	}
	
/*
 void MoveOutsideDiagonal(int indexInMonsterArray) 
{
	 int nextIndex = MonstersArray[indexInMonsterArray].indexPosition + MonstersArray[indexInMonsterArray].lastDirectionAxisX + MonstersArray[indexInMonsterArray].lastDirectionAxisY;
	int saveLastPositionIndex = MonstersArray[indexInMonsterArray].indexPosition;
	int nextIndexX = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisX;
	int nextIndexY = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisY;
	int positionChanged=0;
	if (indexInMonsterArray>= NumberOfMonsters) return;
	if (nextIndex<0 || nextIndex>LENGTH) //upper or lower boarder
	{
		MonstersArray[indexInMonsterArray].lastDirectionAxisY*=-1;;
		MonstersArray[indexInMonsterArray].indexPosition = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisY;
		positionChanged=1;
	}
	if (((nextIndex)%(COL))==0 || ((nextIndex*2)%(COL)==(COL-2) )) //left or right boarder
	{
		MonstersArray[indexInMonsterArray].lastDirectionAxisX*=-1;
		MonstersArray[indexInMonsterArray].indexPosition = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisX;
		positionChanged=1;
	}
	if (TemporaryScreen[nextIndex] == USER) //the monster hits the user
	{
		strike();
		
		return;
	}
	if (TemporaryScreen[nextIndex] == OUTSIDE_DIAGONAL_MONSTER && positionChanged==0) //the monster hits another monster
	{
		MonstersArray[indexInMonsterArray].lastDirectionAxisX*=-1;
		MonstersArray[indexInMonsterArray].lastDirectionAxisY*=-1;
		MonstersArray[indexInMonsterArray].indexPosition = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisX+MonstersArray[indexInMonsterArray].lastDirectionAxisY;
        positionChanged=1;	
	}
	
	if (positionChanged==0) {
		switch (TemporaryScreen[nextIndex+1])
		{
			case FRAME_FILL_COLOR:// move a step
					MonstersArray[indexInMonsterArray].indexPosition = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisX+MonstersArray[indexInMonsterArray].lastDirectionAxisY;
					break;
			
			case FRAME_NOT_FILL_COLOR: //bounce back
			  if ((TemporaryScreen[nextIndexX+1] != FRAME_NOT_FILL_COLOR) && (TemporaryScreen[nextIndexY+1] != FRAME_NOT_FILL_COLOR) )  
			{
				MonstersArray[indexInMonsterArray].lastDirectionAxisX*=-1;
				MonstersArray[indexInMonsterArray].lastDirectionAxisY*=-1;
			}
			else
			{
				if (TemporaryScreen[nextIndexX+1] == FRAME_NOT_FILL_COLOR)
					MonstersArray[indexInMonsterArray].lastDirectionAxisX*=-1;
				if (TemporaryScreen[nextIndexY+1] == FRAME_NOT_FILL_COLOR)
					MonstersArray[indexInMonsterArray].lastDirectionAxisY*=-1;
			}
			
				MonstersArray[indexInMonsterArray].indexPosition = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisX+MonstersArray[indexInMonsterArray].lastDirectionAxisY;
			break;
		}
	}
		// set the old value of the Square in the screen
		TemporaryScreen[saveLastPositionIndex] = ' ';
		TemporaryScreen[saveLastPositionIndex+1] = FRAME_FILL_COLOR;
		//save the current ascii and Attribut
		TemporaryScreen[MonstersArray[indexInMonsterArray].indexPosition] = OUTSIDE_DIAGONAL_MONSTER;
		TemporaryScreen[MonstersArray[indexInMonsterArray].indexPosition+1] =  MONSTER_COLOR|FRAME_FILL_COLOR;
	 
}	
 */


}
